﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Threading;
using System.Windows.Media.Effects;
using System.Runtime.InteropServices;

namespace AleCronoWPF
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class WinCronometro : Window
    {
        Stopwatch watch = new Stopwatch();
        DispatcherTimer timer = new DispatcherTimer();
        DispatcherTimer timerctrlCD = new DispatcherTimer();
        DispatcherTimer timerCD = new DispatcherTimer();
        private System.Windows.Forms.NotifyIcon m_notifyIcon;
        private System.Windows.Forms.ContextMenuStrip m_contextMenu;
        System.Windows.Forms.ToolStripMenuItem mI4 = new System.Windows.Forms.ToolStripMenuItem();
        System.Windows.Forms.ToolStripMenuItem mI5 = new System.Windows.Forms.ToolStripMenuItem();
        System.Windows.Forms.ToolStripMenuItem mI6 = new System.Windows.Forms.ToolStripMenuItem();
        clock.WindowOrologio orologio = new clock.WindowOrologio();
        AleCrono.WindowMinicrono minicrono;
        public static string labcrono = "00:00:00";
        public static long StartTimeCD = 0;
        [DllImport("kernel32.dll")]
        public static extern bool Beep(int BeepFreq, int BeepDuration);

        public WinCronometro()
        {
            InitializeComponent();
        }


        private void btnStart_Stop_Click(object sender, RoutedEventArgs e)
        {
            lblCronometro.Foreground = Brushes.Black;
            if (watch.IsRunning == false)
            {
                this.btnStart_Stop.Content = "Stop";
                mI5.Text = "Stop";
                this.btnStart_Stop.Foreground = Brushes.Red;
                OuterGlowBitmapEffect objOuterGlow = new OuterGlowBitmapEffect();
                objOuterGlow.GlowColor = Colors.Red;
                this.btnStart_Stop.BitmapEffect = objOuterGlow;
                btnAzz_Giro.Content = "Giro";
                mI6.Text = "Giro";
                btnAzz_Giro.Foreground = Brushes.Blue;
                OuterGlowBitmapEffect objOuterGlow2 = new OuterGlowBitmapEffect();
                objOuterGlow2.GlowColor = Colors.Blue;
                btnAzz_Giro.BitmapEffect = objOuterGlow2;
                timer.Interval = TimeSpan.FromMilliseconds(10);
                timer.Tick += new EventHandler(timertick);
                watch.Start();
                timer.Start();
            }
            else if (watch.IsRunning == true)
            {
                this.btnStart_Stop.Content = "Start";
                mI5.Text = "Start";
                this.btnStart_Stop.Foreground = Brushes.DarkGreen;
                OuterGlowBitmapEffect objOuterGlow = new OuterGlowBitmapEffect();
                objOuterGlow.GlowColor = Colors.LawnGreen;
                this.btnStart_Stop.BitmapEffect = objOuterGlow;
                btnAzz_Giro.Content = "Azzera";
                mI6.Text = "Azzera";
                btnAzz_Giro.Foreground = Brushes.LightGray;
                OuterGlowBitmapEffect objOuterGlow3 = new OuterGlowBitmapEffect();
                objOuterGlow3.GlowColor = Colors.LightGray;
                btnAzz_Giro.BitmapEffect = objOuterGlow3;
                watch.Stop();
                timer.Stop();
            }
            this.btnAzz_Giro.IsEnabled = true;
            mI6.Enabled = true;
        }
        

        private void expGiri_Expanded(object sender, RoutedEventArgs e)
        {
            WinCronometroWin.Height = 347;
        }

        private void expGiri_Collapsed(object sender, RoutedEventArgs e)
        {
            WinCronometroWin.Height = 220;
        }

#region Timer_Tick
        private void timertick(Object sender, EventArgs args)
        {
            if (watch.IsRunning == true)
            {
                TimeSpan ts = watch.Elapsed;
                lblCronometro.Content = String.Format("{0:00}:{1:00}:{2:00}",
            ts.Hours, ts.Minutes, ts.Seconds);
                labcrono = String.Format("{0:00}:{1:00}:{2:00}",
            ts.Hours, ts.Minutes, ts.Seconds);
                
            }
         }

        private void timerctrlCDtick(Object sender, EventArgs args)
        {
            if (StartTimeCD != 0)
            {
                btnStart_Stop.IsEnabled = false;
                btnAzz_Giro.IsEnabled = false;
                mI5.Enabled = false;
                mI6.Enabled = false;
                lblCronometro.Content = Convert.ToString(StartTimeCD);
                lblCronometro.HorizontalAlignment = HorizontalAlignment.Center;
                lblCronometro.Foreground = Brushes.OrangeRed;
                btnMiniCrono.IsEnabled = false;
                mI4.Enabled = false;
                timerCD.IsEnabled = true;
                timerCD.Start();
            }
        }

        private void timerCDtick(Object sender, EventArgs args)
        {
            StartTimeCD--;
            lblCronometro.Content = Convert.ToString(StartTimeCD);
            lblCronometro.HorizontalAlignment = HorizontalAlignment.Center;
            if (StartTimeCD == 0)
            {
                m_notifyIcon.ShowBalloonTip(3000, "AleCrono - CountDown", "CountDown Timer Finito !",System.Windows.Forms.ToolTipIcon.Warning);
                lblCronometro.Content = "FINITO !";
                lblCronometro.Foreground = Brushes.Red;
                timerCD.Stop();
                timerCD.IsEnabled = false;
                Beep(500, 100);

                btnMiniCrono.IsEnabled = true;
                mI5.Enabled = true;
                mI6.Enabled = true;
                mI4.Enabled = true;
                btnStart_Stop.IsEnabled = true;
                btnAzz_Giro.IsEnabled = true;
            }
        }

#endregion

        private void btnAzz_Giro_Click(object sender, RoutedEventArgs e)
        {
            lblCronometro.Foreground = Brushes.Black;
            if (watch.IsRunning == true)
            {
                btnAzz_Giro.Content = "Giro";
                mI6.Text = "Giro";
                OuterGlowBitmapEffect objOuterGlow = new OuterGlowBitmapEffect();
                objOuterGlow.GlowColor = Colors.Blue;
                btnAzz_Giro.BitmapEffect = objOuterGlow;
                listgiri.Items.Add(lblCronometro.Content);
                this.expGiri.Visibility = Visibility.Visible;
            }
            else
            {
                btnAzz_Giro.Content = "Azzera";
                mI6.Text = "Azzera";
                OuterGlowBitmapEffect objOuterGlow = new OuterGlowBitmapEffect();
                objOuterGlow.GlowColor = Colors.LightGray;
                btnAzz_Giro.BitmapEffect = objOuterGlow;
                watch.Reset();
                lblCronometro.Content = "00:00:00";
                labcrono = "00:00:00";
                listgiri.Items.Clear();
                this.expGiri.Visibility = Visibility.Hidden;
                this.expGiri.IsExpanded = false;
                this.btnAzz_Giro.IsEnabled = false;
                this.mI6.Enabled = false;
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (watch.IsRunning == true)
            {
                if (MessageBox.Show("Il Cronometro è in esecuzione ! \n Vuoi chiuderere ugualmente AleCrono?", "AleCrono", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.Yes)
                {
                    this.Close();
                }
            }
            else
            {
                this.Close();
            }
        }

        private void listgiri_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (listgiri.Items.Count > 0)
            {
                Microsoft.Win32.SaveFileDialog saveFileDialog1 = new Microsoft.Win32.SaveFileDialog();
                saveFileDialog1.Filter = "File di Testo (*.txt) | *.txt";
                saveFileDialog1.FileName = "ListaIntertempi";
                saveFileDialog1.Title = "Salva Lista Intertempi - AleCrono";
                Nullable<bool> result = saveFileDialog1.ShowDialog();
                if (result == true)
                {
                    System.IO.StreamWriter salvafile = new System.IO.StreamWriter(saveFileDialog1.FileName);
                    string intestazione = "Lista Intertempi AleCreono del : " + DateTime.Now;
                    salvafile.WriteLine(intestazione);
                    salvafile.WriteLine(String.Empty);
                    string numinter;
                    for (int I = 0; I < listgiri.Items.Count; I++)
                    {
                        numinter = Convert.ToString(I + 1);
                        salvafile.WriteLine(@"Intertempo " + numinter + " : " + Convert.ToString(listgiri.Items[I]));
                    }
                    salvafile.Close();
                }
            }
        }

        private void btnInfo_Click(object sender, RoutedEventArgs e)
        {
            AleCrono.AboutAleCrono formabout = new AleCrono.AboutAleCrono();
            formabout.ShowDialog();
        }

        private void WinCronometro_Loaded(object sender, RoutedEventArgs e)
        {
            //Creo icona Notify e suo menu contestuale
            m_contextMenu = new System.Windows.Forms.ContextMenuStrip();
            System.Windows.Forms.ToolStripMenuItem mI1 = new System.Windows.Forms.ToolStripMenuItem();
            mI1.Text = "Apri";
            mI1.Click += new EventHandler(Open_Notify); 
            
            System.Windows.Forms.ToolStripMenuItem mI2 = new System.Windows.Forms.ToolStripMenuItem();
            mI2.Text = "Esci";
            mI2.Click += new EventHandler(Close_Notify); 
            System.Windows.Forms.ToolStripMenuItem mI3 = new System.Windows.Forms.ToolStripMenuItem();
            mI3.Text = "Orologio";
            mI3.Click += new EventHandler(Open_Orologio);
            mI4.Text = "MiniCrono";
            mI4.Click += new EventHandler(Open_minicrono);
            System.Windows.Forms.ToolStripSeparator sp1 = new System.Windows.Forms.ToolStripSeparator();
            System.Windows.Forms.ToolStripSeparator sp2 = new System.Windows.Forms.ToolStripSeparator();
            //Button Start and Stop
            mI5.Text = "Start";
            mI5.Click += new EventHandler(Start_C);
            mI6.Text = "Azzera";
            mI6.Enabled = false;
            mI6.Click += new EventHandler(Azz_C);

            //Populate ContextMenu
            m_contextMenu.Items.Add(mI1);
            m_contextMenu.Items.Add(sp1);
            m_contextMenu.Items.Add(mI5);
            m_contextMenu.Items.Add(mI6);
            m_contextMenu.Items.Add(sp2);
            m_contextMenu.Items.Add(mI3);
            m_contextMenu.Items.Add(mI4);
            m_contextMenu.Items.Add(mI2);
            //Initalize Notify Icon
            m_notifyIcon = new System.Windows.Forms.NotifyIcon();
            m_notifyIcon.Text = "AleCrono";
            //m_notifyIcon.Icon = new System.Drawing.Icon("cronometro.ico");
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream iconStream = assembly.GetManifestResourceStream("AleCrono.Resources.cronometro.ico");
            System.Drawing.Icon icon = new System.Drawing.Icon(iconStream, new System.Drawing.Size(16, 16));
            //m_notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ProductName + ".exe");
            m_notifyIcon.Icon = icon;
            m_notifyIcon.DoubleClick += new EventHandler(doppioclicknotify);
            m_notifyIcon.ContextMenuStrip = m_contextMenu; //Associate the contextmenustrip with notify icon
            m_notifyIcon.Visible = true;
            //Timer Controllo Settaggio CountDown
            timerctrlCD.Interval = TimeSpan.FromMilliseconds(10);
            timerctrlCD.Tick += new EventHandler(timerctrlCDtick);
            timerctrlCD.Start();
            //Timer CountDown
            timerCD.Interval = TimeSpan.FromMilliseconds(1000);
            timerCD.Tick += new EventHandler(timerCDtick);
            timerCD.IsEnabled = false;
        }


        private void Open_Notify(Object sender, EventArgs args)
        {
            WinCronometroWin.Show();
        }
        private void Close_Notify(Object sender, EventArgs args)
        {
            m_notifyIcon.Visible = false;
            if (minicrono != null)
            {
                minicrono.Close();
            }
            this.Close();
        }

        private void WinCronometroWin_Closed(object sender, EventArgs e)
        {
            orologio.Close();
            m_notifyIcon.Visible = false;
            if (minicrono != null)
            {
                minicrono.Close();
            }
        }

        private void btnOrologio_Click(object sender, RoutedEventArgs e)
        {
            orologio.Show();
        }

        private void Open_Orologio(object sender, EventArgs args)
        {
            orologio.Show();
        }

        private void Open_minicrono(object sender, EventArgs args)
        {
            if (mI4.Text == "MiniCrono")
            {
                if (minicrono == null)
                {
                    minicrono = new AleCrono.WindowMinicrono();
                }
                this.Hide();
                minicrono.Show();
                mI4.Text = "Cronometro";
            }
            else if (mI4.Text == "Cronometro")
            {
                minicrono.Hide();
                this.Show();
                mI4.Text = "MiniCrono";
            }
        }

        private void btnNascond_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();
        }

        private void doppioclicknotify(object sender, EventArgs args)
        {
            this.Show();
        }

        private void btnMiniCrono_Click(object sender, RoutedEventArgs e)
        {
            if (minicrono == null)
            {
                minicrono = new AleCrono.WindowMinicrono();
            }
            minicrono.Show();
            this.Visibility = Visibility.Hidden;
            mI4.Text = "Cronometro";
        }

        private void Start_C(object sender, EventArgs args)
        {
            if (watch.IsRunning == false)
            {
                this.btnStart_Stop.Content = "Stop";
                mI5.Text = "Stop";
                this.btnStart_Stop.Foreground = Brushes.Red;
                OuterGlowBitmapEffect objOuterGlow = new OuterGlowBitmapEffect();
                objOuterGlow.GlowColor = Colors.Red;
                this.btnStart_Stop.BitmapEffect = objOuterGlow;
                btnAzz_Giro.Content = "Giro";
                mI6.Text = "Giro";
                btnAzz_Giro.Foreground = Brushes.Blue;
                OuterGlowBitmapEffect objOuterGlow2 = new OuterGlowBitmapEffect();
                objOuterGlow2.GlowColor = Colors.Blue;
                btnAzz_Giro.BitmapEffect = objOuterGlow2;
                timer.Interval = TimeSpan.FromMilliseconds(10);
                timer.Tick += new EventHandler(timertick);
                watch.Start();
                timer.Start();
            }
            else if (watch.IsRunning == true)
            {
                this.btnStart_Stop.Content = "Start";
                mI5.Text = "Start";
                this.btnStart_Stop.Foreground = Brushes.DarkGreen;
                OuterGlowBitmapEffect objOuterGlow = new OuterGlowBitmapEffect();
                objOuterGlow.GlowColor = Colors.LawnGreen;
                this.btnStart_Stop.BitmapEffect = objOuterGlow;
                btnAzz_Giro.Content = "Azzera";
                mI6.Text = "Azzera";
                btnAzz_Giro.Foreground = Brushes.LightGray;
                OuterGlowBitmapEffect objOuterGlow3 = new OuterGlowBitmapEffect();
                objOuterGlow3.GlowColor = Colors.LightGray;
                btnAzz_Giro.BitmapEffect = objOuterGlow3;
                watch.Stop();
                timer.Stop();
            }
            this.btnAzz_Giro.IsEnabled = true;
            mI6.Enabled = true;
        }
        private void Azz_C(object sender, EventArgs args)
        {
            if (watch.IsRunning == true)
            {
                btnAzz_Giro.Content = "Giro";
                mI6.Text = "Giro";
                OuterGlowBitmapEffect objOuterGlow = new OuterGlowBitmapEffect();
                objOuterGlow.GlowColor = Colors.Blue;
                btnAzz_Giro.BitmapEffect = objOuterGlow;
                listgiri.Items.Add(lblCronometro.Content);
                this.expGiri.Visibility = Visibility.Visible;
            }
            else
            {
                btnAzz_Giro.Content = "Azzera";
                mI6.Text = "Azzera";
                OuterGlowBitmapEffect objOuterGlow = new OuterGlowBitmapEffect();
                objOuterGlow.GlowColor = Colors.LightGray;
                btnAzz_Giro.BitmapEffect = objOuterGlow;
                watch.Reset();
                lblCronometro.Content = "00:00:00";
                labcrono = "00:00:00";
                listgiri.Items.Clear();
                this.expGiri.Visibility = Visibility.Hidden;
                this.expGiri.IsExpanded = false;
                this.btnAzz_Giro.IsEnabled = false;
                this.mI6.Enabled = false;
            }
        }

        private void btnCountDown_Click(object sender, RoutedEventArgs e)
        {
            AleCrono.setcountdown countd = new AleCrono.setcountdown();
            countd.ShowDialog();
        }

        private void MenuItem_SubmenuOpened(object sender, RoutedEventArgs e)
        {
            if (labcrono != "00:00:00")
            {
                btnCountDown.IsEnabled = false;
            }
            else
            {
                btnCountDown.IsEnabled = true;
            }
        }
    }
}
